﻿using System.Reflection;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace DynamicAPI.Base
{
    public class AutoAPIControllerFeatureProvider :
        ControllerFeatureProvider
        //IApplicationFeatureProvider<ControllerFeature>, IApplicationFeatureProvider
    {
        //public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
        //{
        //    foreach (var part in parts.OfType<IApplicationPartTypeProvider>())
        //    {
        //        foreach (var type in part.Types)
        //        {
        //            if (IsController(type) && !feature.Controllers.Contains(type))
        //            {
        //                feature.Controllers.Add(type);
        //            }
        //        }
        //    }
        //}

        //protected  override bool IsController(TypeInfo typeInfo)
        protected override bool IsController(TypeInfo typeInfo)
        {
            //判断是否继承了指定的接口
            if (typeof(IAutoAPIService).IsAssignableFrom(typeInfo))
            {
                if (!typeInfo.IsInterface &&
                    !typeInfo.IsAbstract &&
                    !typeInfo.IsGenericType &&
                    typeInfo.IsPublic)
                {
                    return true;
                }
            }

            return false;

            //return base.IsController(typeInfo);
        }

    }
}
